<?php
/*
* @Descripttion: 插件父类
* @Author: jiosen <4631458@qq.com>
* @Date: 2024-06-18 10:24:35
*/
namespace plugins;

use app\model\Plugins;
use think\facade\Cache;
use Exception;

class Base
{
    private $pluginCacheKey = 'PLUGIN_CACHE_LISTS';

    // 插件信息
    protected $config = [];

    // 配置
    protected $setting = [];
    // 回复
    public function handle($data)
    {
        return false;
    }

    // 获取插件信息
    final public function getConfig()
    {
        $config =  $this->config;

        // 默认值
        !isset($config['title']) && $config['title'] = '无标题';
        // !isset($config['img']) && $config['img'] = '';
        $config['img'] = isset($config['img']) ? $this->getPluginStatic($config['img']) : '';
        (!isset($config['setting']) || !in_array($config['setting'],[0,1])) && $config['setting'] = 0;
        !isset($config['author']) && $config['author'] = '匿名';
        !isset($config['tag']) && $config['tag'] = [];

        $this->config = $config;

        return $config;
    }

    // 获取配置
    final public function getSetting($name)
    {
        if(!$name){
            return ['state'=>0,'msg'=>'参数错误'];
        }

        $name = strtolower($name);

        $plugin = Plugins::where('name',$name)->find();
        
        if(empty($plugin)){
            return ['state'=>0,'msg'=>'插件未安装'];
        }

        $class = "\\plugins\\{$name}\\Main";

        if(!class_exists($class)){
            return ['state'=>0,'msg'=>'找不到插件'];
        }

        $obj = new $class;
        $config = $obj->getConfig();
        if($config['setting'] != 1 || empty($obj->setting)){
            return ['state'=>0,'msg'=>'插件没有配置项'];
        }

        if(!empty($plugin->config)){
            foreach($obj->setting as $key => &$value){
                $value['value'] = isset($plugin->config[$key]) ? $plugin->config[$key] : (isset($value['value']) ? $value['value'] : '');
            }
        }

        return ['state'=>1,'data'=>$obj->setting];
    }

    // 修改配置
    final public function setSetting($data)
    {
        if(empty($data['plugin_class'])){
            return ['state'=>0,'msg'=>'参数错误'];
        }

        $name = strtolower($data['plugin_class']);

        $plugin = Plugins::where('name',$name)->find();
        
        if(empty($plugin)){
            return ['state'=>0,'msg'=>'插件未安装'];
        }

        $class = "\\plugins\\{$name}\\Main";

        if(!class_exists($class)){
            return ['state'=>0,'msg'=>'找不到插件'];
        }

        $obj = new $class;
        $config = $obj->getConfig();
        if($config['setting'] != 1 || empty($obj->setting)){
            return ['state'=>0,'msg'=>'插件没有配置项'];
        }

        // 保存
        $plugin->config = $data;

        if($plugin->save()){
            Cache::delete($this->pluginCacheKey);
            return ['state'=>1,'data'=>$obj->setting];
        }

        return ['state'=>0,'msg'=>'保存失败'];

    }

    // 插件列表
    final public function getPlugins()
    {
        $plugins = [];
        
        $dirs = glob(app()->getRootPath() . 'public/plugins/*', GLOB_ONLYDIR);

        foreach ($dirs as $dir) {
            $dir = basename($dir);
            $class = "\\plugins\\{$dir}\\Main";

            if(!class_exists($class)){
                continue;
            }
            
            try{
                $config = (new $class($dir))->getConfig();
                $config['class'] = $dir;
                $plugins[] = $config;
            }catch(Exception $e){
                continue;
            }

        }

        return $plugins;
    }   

    // 获取已安装的插件
    final public function getInstallPlugins()
    {
        $lists = Cache::get($this->pluginCacheKey);

        if(!$lists){
            $lists = Plugins::select()->toArray();
            Cache::set($this->pluginCacheKey,$lists);
        }

        return $lists;
    }

    // 安装插件
    final public function install($name)
    {
        if(!$name){

            return ['state'=>0,'msg'=>'参数错误'];
        }

        $name = strtolower($name);

        $plugin = Plugins::where('name',$name)->find();
        
        if(!empty($plugin)){
            return ['state'=>0,'msg'=>'插件已安装'];
        }

        $class = "\\plugins\\{$name}\\Main";

        if(!class_exists($class)){
            return ['state'=>0,'msg'=>'找不到插件'];
        }

        if(Plugins::create(['name'=>$name,'config'=>[]])){
            Cache::delete($this->pluginCacheKey);
            return ['state'=>1];
        }

        return ['state'=>0,'msg'=>'安装失败'];

    }

    // 卸载插件
    final public function uninstall($name)
    {
        $name = strtolower($name);

        $plugin = Plugins::where('name',$name)->find();
        
        if(empty($plugin)){
            return ['state'=>0,'msg'=>'插件未安装'];
        }

        
        if($plugin->delete()){
            Cache::delete($this->pluginCacheKey);
            return ['state'=>1];
        }

        return ['state'=>0,'msg'=>'卸载失败'];

    }
    // 获取插件路径
    private function getPluginStatic($path)
    {
        if (empty($path)) {
            return '';
        }
    
        if (strpos($path, "http") === 0) {
            return $path;
        } else {
            return request()->domain() . '/plugins'  . ( strpos($path, "/") === 0 ? '' : '/' ) . $path;
        }
    }


}
